package com.rbac.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.rbac.admin.entity.SysMenu;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 菜单Mapper接口
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
     * 根据用户ID查询菜单列表
     */
    @Select("SELECT DISTINCT m.* FROM sys_user_role ur " +
            "LEFT JOIN sys_role_menu rm ON ur.role_id = rm.role_id " +
            "LEFT JOIN sys_menu m ON rm.menu_id = m.id " +
            "WHERE ur.user_id = #{userId} AND m.status = 1 AND m.deleted = 0 " +
            "ORDER BY m.parent_id, m.sort")
    List<SysMenu> selectMenusByUserId(@Param("userId") Long userId);

    /**
     * 根据角色ID查询菜单列表
     */
    @Select("SELECT DISTINCT m.* FROM sys_role_menu rm " +
            "LEFT JOIN sys_menu m ON rm.menu_id = m.id " +
            "WHERE rm.role_id = #{roleId} AND m.status = 1 AND m.deleted = 0 " +
            "ORDER BY m.parent_id, m.sort")
    List<SysMenu> selectMenusByRoleId(@Param("roleId") Long roleId);
}